package com.example.rocketmq.listener;


import com.example.rocketmq.dao.OrderDao;
import com.example.rocketmq.pojo.Order;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RocketMQMessageListener(topic = "tp_order_delayed2", consumerGroup = "cs_grp_order_delayed2")
public class RocketDelayedListener implements RocketMQListener<String> {
    @Autowired
    OrderDao orderDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;





    @Override
    public void onMessage(String orderId) {
        System.out.println("等待支付【过期】的消息：" + orderId);
        List<Order> orders = orderDao.queryByOrder_no(orderId);
        if(orders==null || orders.size()==0){
            System.out.println("订单不存在");
        }else{
            Order order = orders.get(0);
            String o_type = order.getO_type();
            if("0".equals(o_type)){
                System.out.println("订单支付超时");
                orderDao.updateStatusById("2",orderId);
                //将库存放回
                Long order1 = stringRedisTemplate.opsForValue().decrement("order", 1);
                System.out.println();

            }else if("1".equals(o_type)){
                System.out.println("订单已支付");
            }
        }


    }
}
